<?php
namespace Fang\Controller;
use MyExt\MainController;
require './ThinkPHP/Library/MyExt/upyun.class.php';

class BeforeController extends MainController{


	public function _initialize(){

	}


	//遍历缓存的上传图片列表 
	public function issue_sess_images(){
		if(session('userInfo')){
			$userInfo = Encrypt(session('userInfo'),1);
			$userid = md5($userInfo['user_account']);
			$this->assign('data',S($userid));
			$this->display();
		}
	}



	//发布图片的删除
	public function issue_sess_delete(){
		if(!Token_V('issue_sess_delete',I('post.token'),'',1)){
			echo 'e1';exit;		//非法访问
		}
		if(!session('userInfo')){
			echo 'e0';exit;		//请先登陆
		}else{
			$userInfo = Encrypt(session('userInfo'),1);
			$userid = md5($userInfo['user_account']);
		}
		$current = I('post.data');
		$u = new \Upyun('fang-images','tpcwre','xiaodong123');				//删除upyun上的图片
		$u->delete($current);
		$sess = S($userid);
		$key = array_search($current,$sess);
		if($key !== false){
			array_splice($sess,$key,1);
		}
		S($userid,$sess);
		echo 'ok';
	}


	//发布图片的添加
	public function issue_img_add(){
		if(!Token_V('issue_img_add',I('post.token'),'',1)){
			echo 'e1';exit;		//非法访问
		}
		

		if(!session('userInfo')){
			echo 'e0';exit;		//请先登陆
		}else{
			$userInfo = Encrypt(session('userInfo'),1);
			$userid = md5($userInfo['user_account']);
		}
		//dump(S($userid));
		//echo count(S($userid));

	//	print_r($userInfo);exit;
		if(!$_FILES[I('post.f_name')]){
			exit;
		}
		$f = $_FILES[I('post.f_name')];
		$f_path = I('post.f_path');
		$f_ext = pathinfo($f['name'],PATHINFO_EXTENSION);
		$f_type = $f['type'];
		$arr_type = array(
			'image/png',
			'image/gif',
			'image/jpeg',
			'jpg',
			'png',
			'gif',
			'JPG',
			'PNG',
			'GIF'
		);
		if(!in_array($f_type,$arr_type)){
			echo 'e1';exit;			//图片类型不正确
		}
		if(!in_array($f_ext,$arr_type)){
			echo 'e2';exit;			//图片后缀不正确
		}

		$f_pre = uniqid().'.';
		$f_name = $f_path.$f_pre.$f_ext;
		$f_res = fopen($_FILES[I('post.f_name')]['tmp_name'],'r');
		$u = new \Upyun('fang-images','tpcwre','xiaodong123');		//创建upyun 对象
		$info = $u->writeFile($f_name,$f_res,true);
		if($info){
			if(S($userid)){
				$sess_img_temp = S($userid);
				array_push($sess_img_temp,$f_name);
				S($userid,$sess_img_temp);
						if(count(S($userid))>5){
							echo 'e4';exit;
						}
			}else{
				$sess_img_temp = array($f_name);
				S($userid,$sess_img_temp);
			}
			//保存到临时
			echo 'ok';
		}else{
			echo 'e3';		//图片上传失败
		}
	}


	//将临时的发布图片清除
	public function issue_sesstemp_clear(){
		if(!session('userInfo')){
			echo 'e0';exit;		//请先登陆
		}else{
			$userInfo = Encrypt(session('userInfo'),1);
			$userid = md5($userInfo['user_account']);
		}
		$u = new \Upyun('fang-images','tpcwre','xiaodong123');
		if(S($userid)){
			$imgs = S($userid);
			S($userid,null);
			echo 'ok';
			foreach($imgs as $v){
				$u->delete($v);
			}
		}
	}












	public function index(){
		$this->display();
	}

	public function index_condition(){

		//Token_V('index_condition',I('get.token'));
		//搜索组件-文本
		$search_text = array(
			'0'=>'搜索类型',
			'1'=>'按标题',
			'2'=>'按内容',
		);
		$this->assign('search_text',$search_text);


		//搜索组件-按钮组
		$search_nav = array(
			'0'=>'全部',
			'1'=>'出售',
			'2'=>'出租',
			'3'=>'求购',
			'4'=>'求租',
		);
		$this->assign('search_nav',$search_nav);


		//搜索组件-下拉列表
		$search_select = array(
			array(
				'key'=>'condition_class',		//户型
				'list'=>array(
					'0'=>'户型选择',	 
					'1'=>'单室',
					'2'=>'两室',
					'3'=>'一室一厅',
					'4'=>'两室一厅',
					'5'=>'三室一厅',
					'6'=>'三室两厅',
					'7'=>'四室两厅',
					'8'=>'五室两厅',
					'9'=>'其它',
				),
			),
			array(
				'key'=>'condition_area',		//面积
				'list'=>array(
					'0'=>'面积选择',
					'1'=>'50平以下',
					'2'=>'50-75平',
					'3'=>'75-100平',
					'4'=>'100-150平',
					'5'=>'150-200平',
					'6'=>'200平以上'
				),
			),

			array(
				'key'=>'condition_price',		//价格
				'list'=>array(
					 '0'=>'价格选择',
					 '1'=>'10万以下',
					 '2'=>'10-20万',
					 '3'=>'20-30万',
					 '4'=>'30-40万',
					 '5'=>'40-50万',
					 '6'=>'50-75万',
					 '7'=>'75-100万',
					 '8'=>'100-150万',
					 '9'=>'150-200万',
					 '10'=>'200-300万',
					 '11'=>'300万以上'
				),
			),
			array(
				'key'=>'condition_price',		//价格
				'list'=>array(
					 '0'=>'价格选择',
					 '12'=>'500元以下',
					 '13'=>'500-1000元',
					 '14'=>'1000-1500元',
					 '15'=>'1500-2000元',
					 '16'=>'2000-3000元',
					 '17'=>'3000-5000元',
					 '18'=>'5000元以上'
				),
			)
		);
		$this->assign('search_select',$search_select);
		session('info_class',$search_select[0]['list']);	//保存，在发布页使用
		$this->display();
		
	}



	public function index_infolist(){
	//	Token_V('index_infoList',I('get.token'));
		$_GET['pnow'] = I('get.condition_pnow')?:1;
		$search_text_content = I('get.condition_SearchText_Content');
		if($search_text_content){
			switch(I('get.condition_SearchText_Type')){
				case '1':
					$where['info_title'] = array('like',"%{$search_text_content}%"); 
				break;
				case '2':
					$where['info_info'] = array('like',"%{$search_text_content}%"); 
				break;
				default:
					$where['info_title | info_info'] = array('like',array("%{$search_text_content}%")); 
			}
		}
		if(I('get.condition_search_nav')){
			$where['info_type'] = I('get.condition_search_nav');
		}
		if(I('get.condition_class')){
			$where['info_hClassId'] = I('get.condition_class');
		}
		if(I('get.condition_area')){
			$where['info_hAreaId'] = I('get.condition_area');
		}
		if(I('get.condition_price')){
			$where['info_hPriceId'] = I('get.condition_price');
		}

		//信息列表
		$m_info = M('HouseInfo');
		$max = $m_info->max('info_id');
		$wgt = intval($max-40000);
		if($wgt > 0){
			$where['info_id'] = array('gt',$wgt);
		}
        $field = array(
            'fields'=>array('info_uid',"info_hClassId",'info_hAreaId','info_hPriceId'),
            'stat'=>1      // stat 有值时为排除字段，无值时为查询指定字段
        );
		//dump($where);
		$data_info = Get_Fpage_Ajax($m_info,$field,$where,'info_id desc',5);
		//dump($data_info);
		$this->assign('data_info',$data_info);
		$this->assign('info_list',json_encode($data_info['list']));
		$this->display();
	}







	//发布信息处理
	public function issue_c(){
		Token_V('issue_c',I('post.token'));
		if(!session('userInfo')){
			echo 'e0';exit;		//请先登陆
		}else{
			$userInfo = Encrypt(session('userInfo'),1);
			$userid = md5($userInfo['user_account']);
		}
		$p = '/^.{2,100}$/';
		if(!preg_match($p,I('post.info_title'))){
			echo 'e1';exit;		//标题不得为空！
		}

		$p2 = '/^[1-9]{1,5}[.]?[0-9]{0,2}$/';
		if(!preg_match($p2,I('post.info_hArea'))){
			echo 'e2';exit;		//面积填写不正确！
		}

		if(I('post.info_type')==2 || I('post.info_type')==4){
			$p = '/^[0-9]{1,5}$/';
		}else{
			$p = '/^[1-9]{1,5}[.]?[0-9]{0,2}$/';
		}
		if(!preg_match($p,I('post.info_hPrice'))){
			echo 'e3';exit;		//价格填写不正确！
		}

		$p = '/^.{0,200}$/';
		if(!preg_match($p,I('post.info_addr'))){
			echo 'e4';exit;		//地址长度不合法！
		}

		$p = '/^.{1,24}$/';
		if(!preg_match($p,I('post.info_contacts'))){
			echo 'e5';exit;		//联系人不得为空！
		}
		$p  = '/^[0-9]{7,12}$/';
		if(!preg_match($p,I('post.info_phone'))){
			echo 'e6';exit;		//电话填写不正确！
		}


		$p = '/^.{0,500}$/';
		if(!preg_match($p,I('post.info_info'))){
			echo 'e7';exit;		//详情输入长度不合法！
		}

		$data = I('post.');
		$data['info_uid'] = $userInfo['user_id'];			//用户id

		$t = date('Y-m-d');
		$stamp = strtotime($t);
		//echo date('Y-m-d H:i:s',$stamp);
		$m = M('HouseInfo');
		$count = $m->count();
		$wgt = intval($count - 40000);
		$f_where['info_ctime'] = array('gt',$stamp);
		$f_where['info_type'] = I('post.info_type');
		$f_where['info_uid'] = 	$data['info_uid'];
		if($wgt > 0){
			$f_where['info_id'] = array('gt',$wgt);
		} 

		$data_find = $m->where($f_where)->find();
		if($data_find){
//			echo 'e8';exit;		//每天只能发布一条信息！
		}

		$data['info_ctime']=time();			//创建时间

		//规划面积的分类
		if($data['info_hArea'] < 50){
			$data['info_hAreaId'] = 1;
		}else if($data['info_hArea'] >=50 && $data['info_hArea'] < 75){
			$data['info_hAreaId'] = 2;
		}else if($data['info_hArea'] >=75 && $data['info_hArea'] < 100){
			$data['info_hAreaId'] = 3;
		}else if($data['info_hArea'] >=100 && $data['info_hArea'] < 150){
			$data['info_hAreaId'] = 4;
		}else if($data['info_hArea'] >=150 && $data['info_hArea'] < 200){
			$data['info_hAreaId'] = 5;
		}else{
			$data['info_hAreaId'] = 6;
		}
		//规划价格的分类
		if($data['info_type'] == 1 || $data['info_type'] == 3){
			if($data['info_hPrice'] < 10){
				$data['info_hPriceId'] = 1;
			}else if($data['info_hPrice'] >=10 && $data['info_hPrice'] < 20){
				$data['info_hPriceId'] = 2;
			}else if($data['info_hPrice'] >=20 && $data['info_hPrice'] < 30){
				$data['info_hPriceId'] = 3;
			}else if($data['info_hPrice'] >=30 && $data['info_hPrice'] < 40){
				$data['info_hPriceId'] = 4;
			}else if($data['info_hPrice'] >=40 && $data['info_hPrice'] < 50){
				$data['info_hPriceId'] = 5;
			}else if($data['info_hPrice'] >=50 && $data['info_hPrice'] < 75){
				$data['info_hPriceId'] = 6;
			}else if($data['info_hPrice'] >=75 && $data['info_hPrice'] < 100){
				$data['info_hPriceId'] = 7;
			}else if($data['info_hPrice'] >=100 && $data['info_hPrice'] < 150){
				$data['info_hPriceId'] = 8;
			}else if($data['info_hPrice'] >=150 && $data['info_hPrice'] < 200){
				$data['info_hPriceId'] = 9;
			}else if($data['info_hPrice'] >=200 && $data['info_hPrice'] < 300){
				$data['info_hPriceId'] = 10;
			}else if($data['info_hPrice'] >= 300){
				$data['info_hPriceId'] = 11;
			}
		}else{
			if($data['info_hPrice'] < 500){
				$data['info_hPriceId'] = 12;
			}else if($data['info_hPrice'] >=500 && $data['info_hPrice'] < 1000){
				$data['info_hPriceId'] = 13;
			}else if($data['info_hPrice'] >=1000 && $data['info_hPrice'] < 1500){
				$data['info_hPriceId'] = 14;
			}else if($data['info_hPrice'] >=1500 && $data['info_hPrice'] < 2000){
				$data['info_hPriceId'] = 15;
			}else if($data['info_hPrice'] >=2000 && $data['info_hPrice'] < 3000){
				$data['info_hPriceId'] = 16;
			}else if($data['info_hPrice'] >=3000 && $data['info_hPrice'] < 5000){
				$data['info_hPriceId'] = 17;
			}else if($data['info_hPrice'] >=5000){
				$data['info_hPriceId'] = 18;
			}
		}
		switch(I('post.issue_class')){
			case 'b':
			case 'd':
				$data['info_hPrice'] = $data['info_hPrice'].'元';
			break;
			default:
				$data['info_hPrice'] = $data['info_hPrice'].'万元';

		}
		if(S($userid)){
			$data['info_imgs'] = json_encode(S($userid));
		}
		if($m->create($data)){
			$stat = $m->add();
			if($stat > 0){
				if($data['info_imgs']){
					S($userid,null);
				}
				echo 'ok';
			}else{
				echo 'e9';		//操作失败
			}
		}
	}













	//获取我的信息列表
	public function me_main_info_list(){
		$userInfo = Encrypt(session('userInfo'),1);
		if(!$userInfo){
			echo "<h1 style='text-align:center;color:red'>非法访问！</h1>";exit;
		}

		$m = M('HouseInfo');
		$max = $m->max('info_id');
		$wgt = intval($max-40000);
		if($wgt > 0){
			$where['info_id'] = array('gt',$wgt);
		}
		$where['info_uid'] = $userInfo['user_id'];
		if(I('get.info_type') && I('get.info_type') > 0){
			$where['info_type'] = I('get.info_type');
		}
        $field = array(
            'fields'=>array('info_uid',"info_hClassId",'info_hAreaId','info_hPriceId'),
            'stat'=>1      // stat 有值时为排除字段，无值时为查询指定字段
        );
		$data_info =Get_Fpage_Ajax($m,$fields,$where,'info_id desc',5);
		$this->assign('data_info',$data_info);
		$this->assign('info_list',json_encode($data_info['list']));
	//	dump($data_info['list']);
		$this->display();
	}



	//删除指定信息
	public function me_main_info_list_del(){
		Token_V('me_main_info_list_del',I('post.token'));

		$info_id = I('post.info_id');
		$stat = M('HouseInfo')->delete($info_id);
		if($stat){
			echo 'ok';
			if(I('post.info_imgs') && count(json_decode($_POST['info_imgs'],1)) > 0){
				$imgs = json_decode($_POST['info_imgs']);
				$u = new \Upyun('fang-images','tpcwre','xiaodong123');
				foreach($imgs as $v){
					$u->delete($v);
				}
			}
		}
	}


}